package com.example.administrator.dc.fragment;


import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.dc.Bean.SendTelcode;
import com.example.administrator.dc.Bean.Verificationresult;
import com.example.administrator.dc.R;
import com.example.administrator.dc.Service.RegService;
import com.example.administrator.dc.Service.loginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegFragmentone extends BaseFragment {
    private EditText phone1;
    private EditText verification;
    private Button nextstep;
    private Button send;
    private  RegFragmentone regFragmentone;
    private RegFragmenttwo regFragmenttwo;
    private android.support.v4.app.FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;


    public void onViewCreated(View view,@Nullable Bundle savedInstanceState) {
        fragmentManager=getChildFragmentManager();
        phone1= (EditText) view.findViewById(R.id.editText7);
        verification= (EditText) view.findViewById(R.id.editText8);
        nextstep= (Button) view.findViewById(R.id.button3);
        send= (Button) view.findViewById(R.id.button2);
        Retrofit retrofit=new Retrofit.Builder().baseUrl("http://118.89.227.169/home/Api/").
                addConverterFactory(GsonConverterFactory.create()).build();
        final RegService retrofitSerivce= retrofit.create(RegService.class);


        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String telephone=phone1.getText().toString();
                phone=telephone;
                Call<SendTelcode> call=retrofitSerivce.sendtelcode(phone);
                call.enqueue(new Callback<SendTelcode>() {
                    @Override
                    public void onResponse(Call<SendTelcode> call, Response<SendTelcode> response) {
                        if(response.body().getStatus()==1){
                            Toast.makeText(getActivity(),"发送成功",Toast.LENGTH_LONG).show();
                        }

                    }

                    @Override
                    public void onFailure(Call<SendTelcode> call, Throwable t) {

                    }
                });

            }
        });

        nextstep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String telephone=phone1.getText().toString();
                String verificationcode=verification.getText().toString();
                safecode=verificationcode;
                Call<Verificationresult> call=retrofitSerivce.verificationresult(telephone,verificationcode);
                call.enqueue(new Callback<Verificationresult>() {
                    @Override
                    public void onResponse(Call<Verificationresult> call, Response<Verificationresult> response) {
                        if(response.body().getStatus()==1){
                            Toast.makeText(getActivity(),"验证成功",Toast.LENGTH_LONG).show();
                            fragmentTransaction = fragmentManager.beginTransaction();
                            regFragmenttwo = new RegFragmenttwo();
                            regFragmentone=new RegFragmentone();
                            fragmentTransaction.hide(regFragmentone);
                            fragmentTransaction.replace(R.id.fragmentone, regFragmenttwo);
                            nextstep.setVisibility(View.GONE);
                            phone1.setVisibility(View.GONE);
                            send .setVisibility(View.GONE);
                            verification.setVisibility(View.GONE);
                            fragmentTransaction.commit();


                        }
                    }

                    @Override
                    public void onFailure(Call<Verificationresult> call, Throwable t) {

                    }
                });

            }
        });



    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_reg_fragmentone, container, false);
    }

}
